﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TheOutLetLibrary.Dominio;
using TheOutLetLibrary.Infraestrutura;
using System.Data.OleDb;

namespace TheOutLetLibrary.Infraestrutura {

    public class Accessk_productDAO : BaseDAO, IGenericDAO<k_product> {

        public Accessk_productDAO(OleDbTransaction transacao)
            : base(transacao) {
        }

        public k_product Ache(int Id) {

            OleDbCommand comando = GetCommand();

            comando.CommandText =
                "SELECT * FROM k_product WHERE Id = @productId";
            comando.Parameters.Add("@productId", OleDbType.Integer).Value = Id;
            OleDbDataReader reader = comando.ExecuteReader();
            if(reader.HasRows) {
                reader.Read();
                k_product produtoAchado = new k_product();
                produtoAchado.COD = reader.GetInt32(reader.GetOrdinal("COD"));
                produtoAchado.Id = Id;
                // todo: acabar de preencher k_product
                return produtoAchado;
            } else {
                return null;
            }
        }

        private void Altere(k_product product) {
            // todo: implementar rotina de alteração
        }

        public int Inclua(k_product entity) {

            k_product productNaBase = Ache(entity.Id);

            if(productNaBase == null) {

                OleDbCommand comando = GetCommand();
                comando.CommandText =
                    " INSERT INTO k_product (" +
                    " Id, ExternalId, ProductName, DateCreated, LastUpdated, ShortName, " +
                    " SalePrice, DefaultPrice, InventoryTracking, Saleable, Hidden, " + 
                    " VariationMultiplicity, IsKit, CategoryId, BrandId) " +
                    " VALUES (" +
                    " @Id, @ExternalId, @ProductName, @DateCreated, @LastUpdated, @ShortName, " +
                    " @SalePrice, @DefaultPrice, @InventoryTracking, @Saleable, @Hidden, " +
                    " @VariationMultiplicity, @IsKit, @CategoryId, @BrandId) ";
                comando.Parameters.Add("@Id", OleDbType.Integer).Value = entity.Id; ;
                comando.Parameters.Add("@ExternalId", OleDbType.VarChar).Value = entity.ExternalId;
                comando.Parameters.Add("@ProductName", OleDbType.VarChar).Value = entity.ProductName;
                comando.Parameters.Add("@DateCreated", OleDbType.Date).Value = entity.DateCreated;
                comando.Parameters.Add("@LastUpdated", OleDbType.Date).Value = entity.LastUpdated;
                comando.Parameters.Add("@ShortName", OleDbType.VarChar).Value = entity.ShortName;
                comando.Parameters.Add("@SalePrice", OleDbType.Currency).Value = entity.SalePrice;
                comando.Parameters.Add("@DefaultPrice", OleDbType.Currency).Value = entity.DefaultPrice;
                comando.Parameters.Add("@InventoryTracking", OleDbType.Boolean).Value = entity.InventoryTracking;
                comando.Parameters.Add("@Saleable", OleDbType.Boolean).Value = entity.Saleable;
                comando.Parameters.Add("@Hidden", OleDbType.Boolean).Value = entity.Hidden;
                comando.Parameters.Add("@VariationMultiplicity", OleDbType.VarChar).Value = entity.VariationMultiplicity;
                comando.Parameters.Add("@IsKit", OleDbType.Boolean).Value = entity.IsKit;
                comando.Parameters.Add("@CategoryId", OleDbType.Integer).Value = entity.Category.Id;
                comando.Parameters.Add("@BrandId", OleDbType.Integer).Value = entity.Brand.Id;
                comando.ExecuteNonQuery();

                comando.CommandText = "SELECT @@IDENTITY";
                entity.COD = Convert.ToInt32(comando.ExecuteScalar());
                return entity.COD;

            } else {
                Altere(entity);
                return productNaBase.COD;
            }

        }



    }
}
